package com.example.gss.controller;

import com.example.gss.entity.Student;
import com.example.gss.Service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework. http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 创建学生
     */
    @PostMapping
    public ResponseEntity<Student> createStudent(@RequestBody Student student) {
        Student createdStudent = studentService.createStudent(student);
        return new ResponseEntity<>(createdStudent, HttpStatus.CREATED);
    }

    /**
     * 获取所有学生
     */
    @GetMapping
    public ResponseEntity<List<Student>> getAllStudents() {
        List<Student> students = studentService.getAllStudents();
        return new ResponseEntity<>(students, HttpStatus.OK);
    }

    /**
     * 根据ID获取学生
     */
    @GetMapping("/{studentId}")
    public ResponseEntity<Student> getStudentById(@PathVariable String studentId) {
        Optional<Student> studentOptional = studentService.getStudentById(studentId);
        return studentOptional.map(student -> new ResponseEntity<>(student, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * 更新学生
     */
    @PutMapping("/{studentId}")
    public ResponseEntity<Student> updateStudent(@PathVariable String studentId, @RequestBody Student student) {
        student.setStudentId(studentId);
        Student updatedStudent = studentService.updateStudent(student);
        return new ResponseEntity<>(updatedStudent, HttpStatus.OK);
    }

    /**
     * 删除学生
     */
    @DeleteMapping("/{studentId}")
    public ResponseEntity<Void> deleteStudent(@PathVariable String studentId) {
        studentService.deleteStudent(studentId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 根据姓名查询学生
     */
    @GetMapping("/search/name")
    public ResponseEntity<List<Student>> findStudentsByName(@RequestParam String name) {
        List<Student> students = studentService.findStudentsByName(name);
        return new ResponseEntity<>(students, HttpStatus.OK);
    }

    /**
     * 根据年级查询学生
     */
    @GetMapping("/search/grade")
    public ResponseEntity<List<Student>> findStudentsByGrade(@RequestParam String grade) {
        List<Student> students = studentService.findStudentsByGrade(grade);
        return new ResponseEntity<>(students, HttpStatus.OK);
    }

    /**
     * 根据导师ID查询学生
     */
    @GetMapping("/search/tutor")
    public ResponseEntity<List<Student>> findStudentsByTutorId(@RequestParam String tutorId) {
        List<Student> students = studentService.findStudentsByTutorId(tutorId);
        return new ResponseEntity<>(students, HttpStatus.OK);
    }

    /**
     * 查询所有未休学的学生
     */
    @GetMapping("/active")
    public ResponseEntity<List<Student>> findActiveStudents() {
        List<Student> students = studentService.findActiveStudents();
        return new ResponseEntity<>(students, HttpStatus.OK);
    }
}